<?php
namespace Application\Modelservice;

use Zend\Db\TableGateway\TableGateway;


class PacienteTable
{
    protected $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    public function fetchAll()
    {
        $resultSet = $this->tableGateway->select();
        return $resultSet;
    }

    public function getPaciente($id)
    {
        $id  = (int) $id;
        $rowset = $this->tableGateway->select(array('id' => $id));
        $row = $rowset->current();
        if (!$row) {
            throw new \Exception("Could not find row $id");
        }
        return $row;
    }

    public function savePaciente(Paciente $paciente)
    {
        $data = array(
            'nome' => $paciente->nome,
            'CPF' => $paciente->CPF,
            'email'=> $paciente->email,
            'sexo'=> $paciente->sexo,
            'telefone'=> $paciente->telefone,
            'login' => $paciente->login,
            'senha' => $paciente->senha,
            'ativado' => $paciente->ativado,            
        );

        $id = (int)$paciente->id;
        if ($id == 0) {
            $this->tableGateway->insert($data);
        } else {
            if ($this->getPaciente($id)) {
                $this->tableGateway->update($data, array('id' => $id));
            } else {
                throw new \Exception('Form id does not exist');
            }
        }
    }

    public function deletePaciente($id)
    {
        $this->tableGateway->delete(array('id' => $id));
    }
}